package com.aode.music.network;

import android.util.Log;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.aode.common.bean.request.LoginData;
import com.aode.common.bean.request.SearchData;
import com.aode.common.bean.response.Mllist;
import com.aode.common.bean.response.Music;
import com.aode.common.bean.response.User;
import com.aode.common.bean.web.Page;
import com.aode.common.bean.web.Result;
import com.aode.music.service.mllist.MllistService;
import com.aode.music.service.music.MusicService;
import com.aode.music.service.user.UserService;
import com.aode.utils.main.RequestUtils;

import java.util.LinkedHashMap;
import java.util.Map;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.RequestBody;

public class HttpData {

    private static MutableLiveData<Result> resultMutableLiveData = new MutableLiveData<>();
    private static MutableLiveData<Result<User>> userMutableLiveData = new MutableLiveData<>();
    private static MutableLiveData<Result<Page<Mllist>>> mllistMutableLiveData = new MutableLiveData<>();
    private static MutableLiveData<Result<Page<Music>>> musicMutableLiveData = new MutableLiveData<>();
    private static MutableLiveData<Result<Page<Music>>> musicMutableLiveData1 = new MutableLiveData<>();
    private static MutableLiveData<Result<User>> userMutableLiveData1 = new MutableLiveData<>();

    public static MutableLiveData<Result<User>> getUser(SearchData searchData){

        HttpsUtils.getInstance().create(UserService.class)
                .getUser(searchData.getSearch())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Result<User>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull Result<User> userResult) {
                            if(userResult.getCode().equals("0")){
                                userMutableLiveData1.postValue(userResult);
                            }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
        return userMutableLiveData1;
    }


    //注册
    public static MutableLiveData<Result> Register(LoginData loginData){
        Map<String, Object> params = new LinkedHashMap<>();
        params.put("userName", loginData.getUsername());
        params.put("userPassword", loginData.getPassword());
        RequestBody requestBody = RequestUtils.request(params);
        HttpsUtils.getInstance().create(UserService.class)
                .register(requestBody)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Result>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull Result result) {
                        resultMutableLiveData.postValue(result);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
        return  resultMutableLiveData;
    }

    //登录
    public static MutableLiveData<Result<User>> Login(LoginData loginData){
        Map<String, Object> params = new LinkedHashMap<>();
        params.put("userName", loginData.getUsername());
        params.put("userPassword", loginData.getPassword());
        RequestBody requestBody = RequestUtils.request(params);
        HttpsUtils.getInstance().create(UserService.class)
                .login(requestBody)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Result<User>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull Result<User> userResult) {
                        userMutableLiveData.postValue(userResult);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        Result<User> result = new Result<>();
                        result.setCode("-1");
                        result.setMsg(e.getMessage());
                        userMutableLiveData.postValue(result);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
            return userMutableLiveData;
    }



    /**
     * 歌单
     * @param searchData
     * @return
     */
    public static MutableLiveData<Result<Page<Mllist>>> getMllist(SearchData searchData){

        HttpsUtils.getInstance().create(MllistService.class)
                .getMllist(searchData.getPageNum(), searchData.getPageSize(), searchData.getSearch())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Result<Page<Mllist>>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                    }

                    @Override
                    public void onNext(@NonNull Result<Page<Mllist>> pageResult) {
                        Log.e("TAG",pageResult.getData()+"");
                        mllistMutableLiveData.postValue(pageResult);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        Result<Page<Mllist>> pageResult= new Result<>();
                        pageResult.setCode("-1");
                        pageResult.setMsg(e.getMessage());
                        mllistMutableLiveData.postValue(pageResult);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
             return mllistMutableLiveData;
    }

    public static MutableLiveData<Result<Page<Music>>> getMusic(SearchData searchData){
        HttpsUtils.getInstance().create(MusicService.class)
                .getMusic(searchData.getPageNum(), searchData.getPageSize(), searchData.getSearch())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Result<Page<Music>>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull Result<Page<Music>> pageResult) {
                        Log.e("TAG",pageResult.getData().getRecords().size()+"");
                        musicMutableLiveData.postValue(pageResult);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        Result<Page<Music>> pageResult= new Result<>();
                        pageResult.setCode("-1");
                        pageResult.setMsg(e.getMessage());
                        musicMutableLiveData.postValue(pageResult);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
        return musicMutableLiveData;
    }
    public static MutableLiveData<Result<Page<Music>>> getMusic1(SearchData searchData){
        HttpsUtils.getInstance().create(MusicService.class)
                .getMusic1(searchData.getPageNum(), searchData.getPageSize(), searchData.getSearch())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Result<Page<Music>>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull Result<Page<Music>> pageResult) {
                        Log.e("TAG",pageResult.getData().getRecords().size()+"");
                        musicMutableLiveData1.postValue(pageResult);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        Result<Page<Music>> pageResult= new Result<>();
                        pageResult.setCode("-1");
                        pageResult.setMsg(e.getMessage());
                        musicMutableLiveData1.postValue(pageResult);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
        return musicMutableLiveData1;
    }





}
